<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户id" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单信息" prop="orderSerial">
        <el-input
          v-model="queryParams.orderSerial"
          placeholder="请输入订单信息"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="invoiceStatus">
        <el-select v-model="queryParams.invoiceStatus" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.invoice_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="开票金额" prop="invoiceAmount">
        <el-input
          v-model="queryParams.invoiceAmount"
          placeholder="请输入开票金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发票类型" prop="invoiceType">
        <el-select v-model="queryParams.invoiceType" placeholder="请选择发票类型" clearable>
          <el-option
            v-for="dict in dict.type.invoice_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="发票抬头类型" prop="invoiceTitle">
        <el-select v-model="queryParams.invoiceTitle" placeholder="请选择发票抬头类型" clearable>
          <el-option
            v-for="dict in dict.type.invoice_title"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="发票抬头" prop="invoiceName">
        <el-input
          v-model="queryParams.invoiceName"
          placeholder="请输入发票抬头"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="纳税人识别码" prop="ratepayerCode">
        <el-input
          v-model="queryParams.ratepayerCode"
          placeholder="请输入纳税人识别码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开户行" prop="openBank">
        <el-input
          v-model="queryParams.openBank"
          placeholder="请输入开户行"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="银行账户" prop="bankAccount">
        <el-input
          v-model="queryParams.bankAccount"
          placeholder="请输入银行账户"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="企业地址" prop="firmAddress">
        <el-input
          v-model="queryParams.firmAddress"
          placeholder="请输入企业地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="企业电话" prop="invoicePhone">
        <el-input
          v-model="queryParams.invoicePhone"
          placeholder="请输入企业电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="邮寄邮箱" prop="invoiceEmail">
        <el-input
          v-model="queryParams.invoiceEmail"
          placeholder="请输入邮寄邮箱"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发票内容" prop="invoiceDetails">
        <el-input
          v-model="queryParams.invoiceDetails"
          placeholder="请输入发票内容"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="失败原因" prop="invoiceFail">
        <el-input
          v-model="queryParams.invoiceFail"
          placeholder="请输入失败原因"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="crTime">
        <el-date-picker clearable
          v-model="queryParams.crTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="更新时间" prop="upTime">
        <el-date-picker clearable
          v-model="queryParams.upTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择更新时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['store:invoiceinfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['store:invoiceinfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['store:invoiceinfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['store:invoiceinfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="invoiceinfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户ID" align="center" prop="invoiceId" />
      <el-table-column label="用户id" align="center" prop="userId" />
      <el-table-column label="订单信息" align="center" prop="orderSerial" />
      <el-table-column label="状态" align="center" prop="invoiceStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.invoice_status" :value="scope.row.invoiceStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="开票金额" align="center" prop="invoiceAmount" />
      <el-table-column label="发票类型" align="center" prop="invoiceType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.invoice_type" :value="scope.row.invoiceType"/>
        </template>
      </el-table-column>
      <el-table-column label="发票抬头类型" align="center" prop="invoiceTitle">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.invoice_title" :value="scope.row.invoiceTitle"/>
        </template>
      </el-table-column>
      <el-table-column label="发票抬头" align="center" prop="invoiceName" />
      <el-table-column label="纳税人识别码" align="center" prop="ratepayerCode" />
      <el-table-column label="开户行" align="center" prop="openBank" />
      <el-table-column label="银行账户" align="center" prop="bankAccount" />
      <el-table-column label="企业地址" align="center" prop="firmAddress" />
      <el-table-column label="企业电话" align="center" prop="invoicePhone" />
      <el-table-column label="邮寄邮箱" align="center" prop="invoiceEmail" />
      <el-table-column label="发票内容" align="center" prop="invoiceDetails" />
      <el-table-column label="失败原因" align="center" prop="invoiceFail" />
      <el-table-column label="创建时间" align="center" prop="crTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.crTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="upTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.upTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['store:invoiceinfo:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['store:invoiceinfo:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改发票信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="订单信息" prop="orderSerial">
          <el-input v-model="form.orderSerial" placeholder="请输入订单信息" />
        </el-form-item>
        <el-form-item label="状态" prop="invoiceStatus">
          <el-select v-model="form.invoiceStatus" placeholder="请选择状态">
            <el-option
              v-for="dict in dict.type.invoice_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="开票金额" prop="invoiceAmount">
          <el-input v-model="form.invoiceAmount" placeholder="请输入开票金额" />
        </el-form-item>
        <el-form-item label="发票类型" prop="invoiceType">
          <el-select v-model="form.invoiceType" placeholder="请选择发票类型">
            <el-option
              v-for="dict in dict.type.invoice_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发票抬头类型" prop="invoiceTitle">
          <el-select v-model="form.invoiceTitle" placeholder="请选择发票抬头类型">
            <el-option
              v-for="dict in dict.type.invoice_title"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发票抬头" prop="invoiceName">
          <el-input v-model="form.invoiceName" placeholder="请输入发票抬头" />
        </el-form-item>
        <el-form-item label="纳税人识别码" prop="ratepayerCode">
          <el-input v-model="form.ratepayerCode" placeholder="请输入纳税人识别码" />
        </el-form-item>
        <el-form-item label="开户行" prop="openBank">
          <el-input v-model="form.openBank" placeholder="请输入开户行" />
        </el-form-item>
        <el-form-item label="银行账户" prop="bankAccount">
          <el-input v-model="form.bankAccount" placeholder="请输入银行账户" />
        </el-form-item>
        <el-form-item label="企业地址" prop="firmAddress">
          <el-input v-model="form.firmAddress" placeholder="请输入企业地址" />
        </el-form-item>
        <el-form-item label="企业电话" prop="invoicePhone">
          <el-input v-model="form.invoicePhone" placeholder="请输入企业电话" />
        </el-form-item>
        <el-form-item label="邮寄邮箱" prop="invoiceEmail">
          <el-input v-model="form.invoiceEmail" placeholder="请输入邮寄邮箱" />
        </el-form-item>
        <el-form-item label="发票内容" prop="invoiceDetails">
          <el-input v-model="form.invoiceDetails" placeholder="请输入发票内容" />
        </el-form-item>
        <el-form-item label="失败原因" prop="invoiceFail">
          <el-input v-model="form.invoiceFail" placeholder="请输入失败原因" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInvoiceinfo, getInvoiceinfo, delInvoiceinfo, addInvoiceinfo, updateInvoiceinfo } from "@/api/store/invoiceinfo";

export default {
  name: "Invoiceinfo",
  dicts: ['invoice_title', 'invoice_status', 'invoice_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 发票信息表格数据
      invoiceinfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        orderSerial: null,
        invoiceStatus: null,
        invoiceAmount: null,
        invoiceType: null,
        invoiceTitle: null,
        invoiceName: null,
        ratepayerCode: null,
        openBank: null,
        bankAccount: null,
        firmAddress: null,
        invoicePhone: null,
        invoiceEmail: null,
        invoiceDetails: null,
        invoiceFail: null,
        crTime: null,
        upTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询发票信息列表 */
    getList() {
      this.loading = true;
      listInvoiceinfo(this.queryParams).then(response => {
        this.invoiceinfoList = response.data;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        invoiceId: null,
        userId: null,
        orderSerial: null,
        invoiceStatus: null,
        invoiceAmount: null,
        invoiceType: null,
        invoiceTitle: null,
        invoiceName: null,
        ratepayerCode: null,
        openBank: null,
        bankAccount: null,
        firmAddress: null,
        invoicePhone: null,
        invoiceEmail: null,
        invoiceDetails: null,
        invoiceFail: null,
        delFlag: null,
        crTime: null,
        upTime: null,
        invoiceField1: null,
        invoiceField2: null,
        invoiceField3: null,
        invoiceField4: null,
        invoiceField5: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.invoiceId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加发票信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const invoiceId = row.invoiceId || this.ids
      getInvoiceinfo(invoiceId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改发票信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.invoiceId != null) {
            updateInvoiceinfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInvoiceinfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const invoiceIds = row.invoiceId || this.ids;
      this.$modal.confirm('是否确认删除发票信息编号为"' + invoiceIds + '"的数据项？').then(function() {
        return delInvoiceinfo(invoiceIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('store/invoiceinfo/export', {
        ...this.queryParams
      }, `invoiceinfo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
